package cc.mrbird.febs.order.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wuwenze.poi.ExcelKit;

import cc.mrbird.febs.common.annotation.ControllerEndpoint;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.order.entity.OrderInfo;
import cc.mrbird.febs.order.service.IOrderInfoService;
import lombok.RequiredArgsConstructor;

/**
 * 订单表 Controller
 *
 * @author Lord
 * @date 2020-06-06 17:00:35
 */
@Validated
@Controller
@RequiredArgsConstructor
public class OrderInfoController extends BaseController {

    private final IOrderInfoService orderInfoService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "orderInfo")
    public String orderInfoIndex() {
        return FebsUtil.view("orderInfo/orderInfo");
    }

    @GetMapping("orderInfo")
    @ResponseBody
    @RequiresPermissions("orderInfo:list")
    public FebsResponse getAllOrderInfos(OrderInfo orderInfo) {
        return new FebsResponse().success().data(orderInfoService.findOrderInfos(orderInfo));
    }

    @GetMapping("orderInfo/list")
    @ResponseBody
    @RequiresPermissions("orderInfo:list")
    public FebsResponse orderInfoList(QueryRequest request, OrderInfo orderInfo) {
        Map<String, Object> dataTable = getDataTable(this.orderInfoService.findOrderInfos(request, orderInfo));
        return new FebsResponse().success().data(dataTable);
    }

    @ControllerEndpoint(operation = "新增OrderInfo", exceptionMessage = "新增OrderInfo失败")
    @PostMapping("orderInfo")
    @ResponseBody
    @RequiresPermissions("orderInfo:add")
    public FebsResponse addOrderInfo(@Valid OrderInfo orderInfo) {
        this.orderInfoService.createOrderInfo(orderInfo);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "删除OrderInfo", exceptionMessage = "删除OrderInfo失败")
    @GetMapping("orderInfo/delete")
    @ResponseBody
    @RequiresPermissions("orderInfo:delete")
    public FebsResponse deleteOrderInfo(OrderInfo orderInfo) {
        this.orderInfoService.deleteOrderInfo(orderInfo);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改OrderInfo", exceptionMessage = "修改OrderInfo失败")
    @PostMapping("orderInfo/update")
    @ResponseBody
    @RequiresPermissions("orderInfo:update")
    public FebsResponse updateOrderInfo(OrderInfo orderInfo) {
        this.orderInfoService.updateOrderInfo(orderInfo);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改OrderInfo", exceptionMessage = "导出Excel失败")
    @PostMapping("orderInfo/excel")
    @ResponseBody
    @RequiresPermissions("orderInfo:export")
    public void export(QueryRequest queryRequest, OrderInfo orderInfo, HttpServletResponse response) {
        List<OrderInfo> orderInfos = this.orderInfoService.findOrderInfos(queryRequest, orderInfo).getRecords();
        ExcelKit.$Export(OrderInfo.class, response).downXlsx(orderInfos, false);
    }
}
